package com.xyauto.theme.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.xyauto.theme.base.internal.IBaseView
import com.xyauto.theme.utils.AutoUtils
import com.xyauto.theme.utils.DialogUtils

/**
 * Created by gongziyi on 2021/5/13 17:58.
 */
abstract class BaseFragment : Fragment(), IBaseView {


    private var isCreated = false
    private var mFirstLoading = false // 是否第一次加载
    private var bUserVisible = true
    protected lateinit var mContext: Context

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(getLayoutId(), container, false)
        AutoUtils.auto(view)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(savedInstanceState)
        initData()
        initListener()
    }


    protected open fun onGetData() = Unit

    protected open fun lazyData() = Unit

    protected open fun onPageShow() = Unit

    protected open fun onPageHidden() = Unit


    override fun onHiddenChanged(hidden: Boolean) {
        if (hidden) {
            pageHidden()
        } else {
            pageShow()
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (!isCreated) return
        if (isVisibleToUser) pageShow()
        else pageHidden()
    }

    private fun pageShow() {
        onPageShow()
    }

    private fun pageHidden() {
        for (field in javaClass.declaredFields) {
            try {
                field.isAccessible = true
                val o = field[this]
                if (o is EditText) {
                    o.clearFocus()
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        onPageHidden()
    }

    override fun onResume() {
        super.onResume()
        if (!mFirstLoading) {
            lazyData()
            mFirstLoading = true
        }
        bUserVisible = true
    }

    override fun onPause() {
        super.onPause()
        bUserVisible = false
    }


    /**
     * 当前Fragment是否可见（用户在手机屏幕看得见？）。
     *
     * @return
     */
    fun isCurFragmentVisible() = bUserVisible

    override fun showError(msg: String, code: Int) {
        dismissLoadingDialog()
    }

    override fun showLoadingDialog(msg: String?, isCancelable: Boolean) {
        DialogUtils.showLoadingDialog(mContext, msg, isCancelable);
    }

    override fun dismissLoadingDialog() {
        DialogUtils.dismiss();
    }

}